/*
 *  FxBasicInfo.cpp
 *  StrategyInspector
 *
 *  Created by jiro on 12/08/11.
 *  Copyright 2012 jiro music. All rights reserved.
 *
 */

#include "FxBasicInfo.h"


NSString* FxBasicInfo::tableName()
{
	return kText_FxBasicInfo;
}

NSString* FxBasicInfo::queryCreateTable()
{
	NSString* s = [ NSString stringWithFormat: 
		@"create table %@ ( %@ %@, %@ %@, %@ %@, %@ %@, %@ %@, %@ ( %@, %@, %@ ) );",
		tableName(),
		kText_BrokerageFirmIndex, kText_Integer,
		kText_CurrencyCode, kText_Text,
		kText_TargetCurrencyCode, kText_Text,
		kText_Charge, kText_Real,
		kText_Spread, kText_Real,
		kText_PrimaryKey, kText_BrokerageFirmIndex, kText_CurrencyCode, kText_TargetCurrencyCode
	];
	return s;
}

NSString* FxBasicInfo::queryInsertInitialValues()
{
	int index = 0;	//	GMOクリック証券
	std::vector< FxBasicInfo > infos;
	infos.push_back( FxBasicInfo( index, @"USD", @"JPY", 0.0, 0.4 ) );
	infos.push_back( FxBasicInfo( index, @"EUR", @"JPY", 0.0, 0.8 ) );
	infos.push_back( FxBasicInfo( index, @"AUD", @"JPY", 0.0, 1.2 ) );
	infos.push_back( FxBasicInfo( index, @"NZD", @"JPY", 0.0, 1.8 ) );
	infos.push_back( FxBasicInfo( index, @"CAD", @"JPY", 0.0, 2.4 ) );
	infos.push_back( FxBasicInfo( index, @"GBP", @"JPY", 0.0, 1.3 ) );
	infos.push_back( FxBasicInfo( index, @"CHF", @"JPY", 0.0, 2.4 ) );
	infos.push_back( FxBasicInfo( index, @"ZAR", @"JPY", 0.0, 1.9 ) );
	infos.push_back( FxBasicInfo( index, @"EUR", @"USD", 0.0, 0.6 ) );
	infos.push_back( FxBasicInfo( index, @"GBP", @"USD", 0.0, 1.5 ) );
	infos.push_back( FxBasicInfo( index, @"AUD", @"USD", 0.0, 1.5 ) );
	infos.push_back( FxBasicInfo( index, @"GBP", @"CHF", 0.0, 1.8 ) );
	infos.push_back( FxBasicInfo( index, @"EUR", @"CHF", 0.0, 1.8 ) );
	infos.push_back( FxBasicInfo( index, @"USD", @"CHF", 0.0, 2.9 ) );
	
	NSMutableString* s = [ NSMutableString stringWithFormat: 
		@"insert into %@ values ", tableName() ];
	for ( int i = 0, count = sizeof( infos ) / sizeof( FxBasicInfo ); i < count; i++ )
	{
		FxBasicInfo info = infos[ i ];
		[ s appendString: info.queryInsertValue() ];
		if ( i != count - 1 )
		{
			[ s appendString: @"," ];
		}
	}
	[ s appendString: @";" ];
	return s;
}

NSString* FxBasicInfo::querySelectAll( const std::vector< ClauseNode >& clauses )
{
	NSString* s = DbTableEntry::querySelectAll( tableName(), clauses );
	return s;
}

NSString* FxBasicInfo::querySelect( const std::vector< FieldNode >& fieldNodes, const std::vector< ClauseNode >& clauses )
{
	NSString* s = DbTableEntry::querySelect( fieldNodes, tableName(), clauses );
	return s;
}

NSString* FxBasicInfo::queryInsert() const
{
	NSString* s = [ NSString stringWithFormat: 
		@"insert into %@ values %@",
		tableName(), queryInsertValue() ];
	return s;
}

NSString* FxBasicInfo::queryInsertValue() const
{
	NSString* s = [ NSString stringWithFormat: @"( %d, '%@', '%@', %d, %d )",
		brokerageFirmIndex, currencyCode, targetCurrencyCode, charge, spread  
	];
	return s;
}

FxBasicInfo::FxBasicInfo()
{}

FxBasicInfo::FxBasicInfo( int brokerageFirmIndex, NSString* currencyCode, NSString* targetCurrencyCode, double spread, double charge )
	: brokerageFirmIndex( brokerageFirmIndex ),
	  currencyCode( currencyCode ),
	  targetCurrencyCode( targetCurrencyCode ),
	  spread( spread ),
	  charge( charge )
{}
